<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">2022智能制造大赛报名系统</h3>
      <el-form-item prop="phone">
        <el-input  v-model="loginForm.phone"  type="text" auto-complete="off" placeholder="手机号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
          <template slot="append">
            <el-button type="info" @click="getPhoneCode(loginForm.phone)" v-if="get == 0">获取验证码</el-button>
            <el-button type="info" @click="getPhoneCode(loginForm.phone)" v-if="get == 1">重发验证码</el-button>
            <el-button type="info" disabled v-if="get == 2">{{ title }}</el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          type="text"
          auto-complete="off"
          placeholder="验证码">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
<!--      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading" style="font-size: 16px">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
<!--        <div style="float: inherit;" v-if="register">-->
<!--          <router-link class="link-type" :to="'/register'">立即注册</router-link>-->
<!--        </div>-->
        <div align="center" style="margin-top: 5px">
          <el-row type="flex" justify="center">
            <el-col :span="6"><router-link class="link-type link-size" :to="'/login'">账号登陆</router-link></el-col>
            <el-col :span="2" style="color: #bfcbd9">|</el-col>
            <el-col :span="6"><router-link class="link-type link-size" :to="'/register'">注册账号</router-link></el-col>
          </el-row>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2022 www.cmes-imic.org.cn.</span>
    </div>
  </div>
</template>

<script>
import {getCodeImg, getPhoneCode,loginPhone} from "@/api/login";
import Cookies from "js-cookie";
import {encrypt} from "@/utils/jsencrypt";

export default {
  name: "phone",
  data() {
    return {
      // 定时器名称
      timer:null,
      //重复获取
      get:0,
      // 倒计时
      countDown:60,
      title:"",
      codeUrl: "",
      loginForm: {
      },
      loginRules: {
        phone: [
          { required: true, trigger: "blur", message: "请输入您的手机号" },
          {
            pattern: /^1[0-9]\d{9}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    handleLogin(){
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch("Phone", this.loginForm).then(() => {
            console.log('-------',this.redirect);
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            this.$modal.msgError("验证码错误")
          });
        }
      });
    },
    // 获取验证码
    getPhoneCode(phone){
      getPhoneCode(phone).then(res=>{
        if (res.code === 200) {
          this.$message.success('验证码已发送成功')
          // 点击后设置为倒计时
          this.get = 2
          this.title = this.countDown + 's';
          this.loading1() //启动定时器
          this.timer = setInterval(()=>{
            //创建定时
            if (this.countDown == 0){
              this.clearTimer(); //关闭定时器
              this.get = 1
            }else {
              this.loading1()
            }
          },1000)
        }
      })
    },
    loading1(){
      //启动定时器
      this.countDown--; //定时器减1
      this.title = this.countDown + 's';
    },
    //清除定时器
    clearTimer(){
      //清除定时器
      clearInterval(this.timer);
      this.timer = null;
      this.countDown = 60
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.link-size {
  font-size: 16px;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
